package com.bckj.fastboot.core.extra.license.configuration;

import com.bckj.fastboot.core.extra.license.*;
import de.schlichtherle.license.LicenseManager;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * FastLicenseConfiguration
 *
 * @author wfc
 **/
@AutoConfiguration
@ConditionalOnClass(LicenseManager.class)
@EnableConfigurationProperties(LicenseVerifyProperties.class)
public class FastLicenseConfiguration {

    @Bean
    public CustomLicenseManager customLicenseManager(LicenseVerifyProperties properties) {
        return new CustomLicenseManager(properties);
    }

    @Bean
    public LicenseVerifyService licenseVerifyService(LicenseVerifyProperties properties,
                                                     CustomLicenseManager customLicenseManager) {
        LicenseVerifyService licenseVerifyService = new LicenseVerifyServiceImpl(customLicenseManager);
        if (properties.isCacheEnabled()) {
            return new CachedLicenseVerifyServiceImpl(licenseVerifyService,
                    properties.getCacheKey(), properties.getCacheTTL());
        }
        return licenseVerifyService;
    }

    @Bean
    public LicenseCheckAspect licenseCheckAspect(LicenseVerifyService licenseVerifyService) {
        return new LicenseCheckAspect(licenseVerifyService);
    }

}
